package system.model;


import java.util.List;

import com.agile.jfinal.model.CoreModel;
import com.jfinal.ext.plugin.tablebind.TableBind;

/**
* 数据库表对应的Model类，继承了CoreModel
* @author qiutian 
* @date 2013-12-19
*/
@TableBind(tableName="sm_base_type",pkName="pk_base_type") 
public class SmBaseType extends CoreModel<SmBaseType>{

	public static final SmBaseType dao = new SmBaseType();
	
	public static final String PK_BASE_TYPE = "pk_base_type";   //主键
	public static final String VCODE = "vcode";   //类型编码
	public static final String VNAME = "vname";   //名称
	public static final String COMMENTS = "comments";   //备注
	public static final String PK_PARENT = "pk_parent";   //父主键
	public static final String CREATE_BY = "create_by";   //制单人
	public static final String CREATE_DATE = "create_date";   //制单时间
	public static final String OPERATOR = "operator";   //操作员
	public static final String TS = "ts";   //时间戳
	public static final String DR = "dr";   //删除标记
	public static final String COLUMN_1 = "column_1";   //预留字段1
	public static final String COLUMN_2 = "column_2";   //预留字段2
	public static final String COLUMN_3 = "column_3";   //预留字段3
	public static final String COLUMN_4 = "column_4";   //预留字段4
	public static final String COLUMN_5 = "column_5";   //预留字段5
	public static final String ISLEAF = "isleaf";   //是否末级
	
	
	/**
	 * 新增保存方法，非末级节点
	 * @return
	 */
	public boolean addSmBaseTypeModel() {
		// TODO Auto-generated method stub
		
		 //设置父节点为非末级节点
		String pk_parent = get(PK_PARENT);
		SmBaseType parentModel = findById(pk_parent);
		if(parentModel!=null) parentModel.set(ISLEAF, 0).update();
		this.set(ISLEAF, 1);  //设置当前节点为末级
		return save();
	}
	
	/**
	 * 删除数据，并查询当前节点是否有同级节点，如果没有，则父节点变为末级
	 */
	public boolean delSmBaseTypeModel() {
		// TODO Auto-generated method stub
		
		//查询要删除的数据
		SmBaseType smBaseType = queryById();
		
		 //查询当前节点是否有同级节点
		String pk_parent = smBaseType.get(PK_PARENT);
		List listModel = query(PK_PARENT, pk_parent);
		
		if(listModel==null || listModel.size()==0){ //如果没有同级，则父节点变为末级
			SmBaseType parentModel = findById(pk_parent); 
			parentModel.set(ISLEAF, 1).update();
		}else{
			
		}
		return smBaseType.delete();
	}
	
	
	/**数据库中的表名*/
	@Override
	public String getTableName() {
		// TODO Auto-generated method stub
		return "sm_base_type";
	}


	/**主键字段名称*/
	@Override
	public String getPrimaryKey() {
		// TODO Auto-generated method stub
		return PK_BASE_TYPE;
	}
	
}
